{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 生成动态路由轨迹图\n",
    "\n",
    "图书\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from scapy.all import traceroute"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# target=['cn.bing.com','bing.com']\n",
    "target=['www.apple.com.cn',]\n",
    "dport = [80,443] #扫描的端口列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Begin emission:\n",
      "Finished sending 60 packets.\n",
      "\n",
      "Received 62 packets, got 60 answers, remaining 0 packets\n",
      "   115.152.251.162:tcp443 115.152.251.162:tcp80 \n",
      "1  172.17.0.1      11     172.17.0.1      11    \n",
      "2  115.152.251.162 SA     115.152.251.162 SA    \n",
      "3  115.152.251.162 SA     115.152.251.162 SA    \n",
      "4  115.152.251.162 SA     115.152.251.162 SA    \n",
      "5  115.152.251.162 SA     115.152.251.162 SA    \n",
      "6  115.152.251.162 SA     115.152.251.162 SA    \n",
      "7  115.152.251.162 SA     115.152.251.162 SA    \n",
      "8  115.152.251.162 SA     115.152.251.162 SA    \n",
      "9  115.152.251.162 SA     115.152.251.162 SA    \n",
      "10 115.152.251.162 SA     115.152.251.162 SA    \n",
      "11 115.152.251.162 SA     115.152.251.162 SA    \n",
      "12 115.152.251.162 SA     115.152.251.162 SA    \n",
      "13 115.152.251.162 SA     115.152.251.162 SA    \n",
      "14 115.152.251.162 SA     115.152.251.162 SA    \n",
      "15 115.152.251.162 SA     115.152.251.162 SA    \n",
      "16 115.152.251.162 SA     115.152.251.162 SA    \n",
      "17 115.152.251.162 SA     115.152.251.162 SA    \n",
      "18 115.152.251.162 SA     115.152.251.162 SA    \n",
      "19 115.152.251.162 SA     115.152.251.162 SA    \n",
      "20 115.152.251.162 SA     115.152.251.162 SA    \n",
      "21 115.152.251.162 SA     115.152.251.162 SA    \n",
      "22 115.152.251.162 SA     115.152.251.162 SA    \n",
      "23 115.152.251.162 SA     115.152.251.162 SA    \n",
      "24 115.152.251.162 SA     115.152.251.162 SA    \n",
      "25 115.152.251.162 SA     115.152.251.162 SA    \n",
      "26 115.152.251.162 SA     115.152.251.162 SA    \n",
      "27 115.152.251.162 SA     115.152.251.162 SA    \n",
      "28 115.152.251.162 SA     115.152.251.162 SA    \n",
      "29 115.152.251.162 SA     115.152.251.162 SA    \n",
      "30 115.152.251.162 SA     115.152.251.162 SA    \n"
     ]
    }
   ],
   "source": [
    "res,unans = traceroute(target,dport=dport,retry=-2) #启动路由跟踪 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "res.graph(target=\"> test-app4.svg\") #生成svg矢量图形"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[0;31mSignature:\u001b[0m\n",
       "\u001b[0mtraceroute\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\u001b[0m\n",
       "\u001b[0;34m\u001b[0m    \u001b[0mtarget\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n",
       "\u001b[0;34m\u001b[0m    \u001b[0mdport\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m80\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n",
       "\u001b[0;34m\u001b[0m    \u001b[0mminttl\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n",
       "\u001b[0;34m\u001b[0m    \u001b[0mmaxttl\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m30\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n",
       "\u001b[0;34m\u001b[0m    \u001b[0msport\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m<\u001b[0m\u001b[0mRandShort\u001b[0m\u001b[0;34m>\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n",
       "\u001b[0;34m\u001b[0m    \u001b[0ml4\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n",
       "\u001b[0;34m\u001b[0m    \u001b[0mfilter\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n",
       "\u001b[0;34m\u001b[0m    \u001b[0mtimeout\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n",
       "\u001b[0;34m\u001b[0m    \u001b[0mverbose\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n",
       "\u001b[0;34m\u001b[0m    \u001b[0;34m**\u001b[0m\u001b[0mkargs\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n",
       "\u001b[0;34m\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
       "\u001b[0;31mDocstring:\u001b[0m\n",
       "Instant TCP traceroute\n",
       "\n",
       ":param target:  hostnames or IP addresses\n",
       ":param dport:   TCP destination port (default is 80)\n",
       ":param minttl:  minimum TTL (default is 1)\n",
       ":param maxttl:  maximum TTL (default is 30)\n",
       ":param sport:   TCP source port (default is random)\n",
       ":param l4:      use a Scapy packet instead of TCP\n",
       ":param filter:  BPF filter applied to received packets\n",
       ":param timeout: time to wait for answers (default is 2s)\n",
       ":param verbose: detailed output\n",
       ":return: an TracerouteResult, and a list of unanswered packets\n",
       "\u001b[0;31mFile:\u001b[0m      /usr/local/lib/python3.7/dist-packages/scapy/layers/inet.py\n",
       "\u001b[0;31mType:\u001b[0m      function\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "traceroute?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Unanswered: TCP:0 UDP:0 ICMP:0 Other:0>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "unans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "plot() missing 1 required positional argument: 'f'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-13-a9798138f1b2>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mres\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mplot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m: plot() missing 1 required positional argument: 'f'"
     ]
    }
   ],
   "source": [
    "res.plot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
